package org.yysd.util;

import okhttp3.*;
import org.apache.commons.lang3.StringUtils;
import org.yysd.pojo.RequestArgs;
import org.yysd.pojo.ResponseResult;

import java.util.Iterator;
import java.util.Map;

public class HttpRequestUtil implements IHttpRequest {
    private static final OkHttpClient client = new OkHttpClient();

    @Override
    public ResponseResult send(RequestArgs requestArgs) throws Exception {
        ResponseResult responseResult = new ResponseResult();
        String httpMethod = requestArgs.getHttpMethod();
        switch (httpMethod) {
            case "GET":
                responseResult = this.sendGet(requestArgs);
                break;
            case "POST":
                responseResult = this.sendPost(requestArgs);
                break;
            case "PUT":
                responseResult = this.sendPut(requestArgs);
                break;
            case "DELETE":
                responseResult = this.sendDelete(requestArgs);
                break;
            default:
                break;
        }
        return responseResult;
    }

    // 发送GET请求
    @Override
    public ResponseResult sendGet(RequestArgs requestArgs) throws Exception {
        String url = requestArgs.getUrl();
        Map<String, String> headerMap = requestArgs.getHeaderMap();
        Map<String, String> parameMap = requestArgs.getParameMap();
        String jsonBody = requestArgs.getJsonBody();
        //
        HttpUrl.Builder urlBuilder = HttpUrl.parse(url).newBuilder();
        if (parameMap != null) {
            Iterator<Map.Entry<String, String>> iterator = parameMap.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, String> entry = iterator.next();
                if (StringUtils.isNotBlank(entry.getKey())) urlBuilder.addQueryParameter(entry.getKey(), entry.getValue());
            }
        }
        String requestUrl = urlBuilder.build().toString();

        Request.Builder requestBuilder = new Request.Builder()
                .url(requestUrl)
                .get();

        if (headerMap != null) {
            Iterator<Map.Entry<String, String>> headerIterator = headerMap.entrySet().iterator();
            while (headerIterator.hasNext()) {
                Map.Entry<String, String> entry = headerIterator.next();
                if (StringUtils.isNotBlank(entry.getKey())) requestBuilder.addHeader(entry.getKey(), entry.getValue());
            }
        }

        Request request = requestBuilder.build();

        ResponseResult responseResult = new ResponseResult();
        responseResult.setRequestUrl(requestUrl);

        try (Response response = client.newCall(request).execute()) {
            String string = response.body().string();
            responseResult.setBody(string);
            responseResult.setStatus(String.format("%s %s",response.code(),response.message()));
        }

        return responseResult;
    }

    // 发送POST请求
    @Override
    public ResponseResult sendPost(RequestArgs requestArgs) throws Exception {
        String url = requestArgs.getUrl();
        Map<String, String> headerMap = requestArgs.getHeaderMap();
        Map<String, String> parameMap = requestArgs.getParameMap();
        String jsonBody = requestArgs.getJsonBody();
        //
        HttpUrl.Builder urlBuilder = HttpUrl.parse(url).newBuilder();
        if (parameMap != null) {
            Iterator<Map.Entry<String, String>> iterator = parameMap.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, String> entry = iterator.next();
                if (StringUtils.isNotBlank(entry.getKey())) urlBuilder.addQueryParameter(entry.getKey(), entry.getValue());
            }
        }
        String requestUrl = urlBuilder.build().toString();
        //
        RequestBody body = RequestBody.create(jsonBody, MediaType.get("application/json"));

        Request.Builder requestBuilder = new Request.Builder()
                .url(requestUrl)
                .post(body);

        if (headerMap != null) {
            Iterator<Map.Entry<String, String>> headerIterator = headerMap.entrySet().iterator();
            while (headerIterator.hasNext()) {
                Map.Entry<String, String> entry = headerIterator.next();
                if (StringUtils.isNotBlank(entry.getKey())) requestBuilder.addHeader(entry.getKey(), entry.getValue());
            }
        }

        Request request = requestBuilder.build();

        ResponseResult responseResult = new ResponseResult();
        responseResult.setRequestUrl(requestUrl);

        try (Response response = client.newCall(request).execute()) {
            String string = response.body().string();
            responseResult.setBody(string);
            responseResult.setStatus(String.format("%s %s",response.code(),response.message()));
        }

        return responseResult;
    }

    // 发送PUT请求
    @Override
    public ResponseResult sendPut(RequestArgs requestArgs) throws Exception {
        String url = requestArgs.getUrl();
        Map<String, String> headerMap = requestArgs.getHeaderMap();
        Map<String, String> parameMap = requestArgs.getParameMap();
        String jsonBody = requestArgs.getJsonBody();
        //
        HttpUrl.Builder urlBuilder = HttpUrl.parse(url).newBuilder();
        if (parameMap != null) {
            Iterator<Map.Entry<String, String>> iterator = parameMap.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, String> entry = iterator.next();
                if (StringUtils.isNotBlank(entry.getKey())) urlBuilder.addQueryParameter(entry.getKey(), entry.getValue());
            }
        }
        String requestUrl = urlBuilder.build().toString();
        //
        RequestBody body = RequestBody.create(jsonBody, MediaType.get("application/json"));

        Request.Builder requestBuilder = new Request.Builder()
                .url(requestUrl)
                .put(body);

        if (headerMap != null) {
            Iterator<Map.Entry<String, String>> headerIterator = headerMap.entrySet().iterator();
            while (headerIterator.hasNext()) {
                Map.Entry<String, String> entry = headerIterator.next();
                if (StringUtils.isNotBlank(entry.getKey())) requestBuilder.addHeader(entry.getKey(), entry.getValue());
            }
        }

        Request request = requestBuilder.build();

        ResponseResult responseResult = new ResponseResult();
        responseResult.setRequestUrl(requestUrl);

        try (Response response = client.newCall(request).execute()) {
            String string = response.body().string();
            responseResult.setBody(string);
            responseResult.setStatus(String.format("%s %s",response.code(),response.message()));
        }

        return responseResult;
    }

    // 发送DELETE请求
    @Override
    public ResponseResult sendDelete(RequestArgs requestArgs) throws Exception {
        String url = requestArgs.getUrl();
        Map<String, String> headerMap = requestArgs.getHeaderMap();
        Map<String, String> parameMap = requestArgs.getParameMap();
        String jsonBody = requestArgs.getJsonBody();
        //
        HttpUrl.Builder urlBuilder = HttpUrl.parse(url).newBuilder();
        if (parameMap != null) {
            Iterator<Map.Entry<String, String>> iterator = parameMap.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, String> entry = iterator.next();
                if (StringUtils.isNotBlank(entry.getKey())) urlBuilder.addQueryParameter(entry.getKey(), entry.getValue());
            }
        }
        String requestUrl = urlBuilder.build().toString();
        //
        RequestBody body = RequestBody.create(jsonBody, MediaType.get("application/json"));

        Request.Builder requestBuilder = new Request.Builder()
                .url(requestUrl)
                .delete(body);

        if (headerMap != null) {
            Iterator<Map.Entry<String, String>> headerIterator = headerMap.entrySet().iterator();
            while (headerIterator.hasNext()) {
                Map.Entry<String, String> entry = headerIterator.next();
                if (StringUtils.isNotBlank(entry.getKey())) requestBuilder.addHeader(entry.getKey(), entry.getValue());
            }
        }

        Request request = requestBuilder.build();

        ResponseResult responseResult = new ResponseResult();
        responseResult.setRequestUrl(requestUrl);

        try (Response response = client.newCall(request).execute()) {
            String string = response.body().string();
            responseResult.setBody(string);
            responseResult.setStatus(String.format("%s %s",response.code(),response.message()));
        }
        return responseResult;
    }
}
